<?php

class S_Controller extends CI_Controller {
	
	public function __construct()
	{
		header("content-type:text/html; charset=utf-8");

		parent::__construct();
		
		$this->config->add_config_path(FCPATH); //增加配置文件目录
		
		$this->load->helper('common'); //载入一些公用函数
		
		date_default_timezone_set(site('timezone')); //设置时区

		$this->load->helper('url'); //载入ci的url函数库
		
		$this->load->database(); //载入数据库驱动
	}
	
	//是否get请求
	public function is_get()
	{
		return $_SERVER['REQUEST_METHOD'] == 'GET' ? TRUE : FALSE;
	}
	
	//是否post请求
	public function is_post()
	{
		return $_SERVER['REQUEST_METHOD'] == 'POST' ? TRUE : FALSE;
	}
	
	//判断是否ajax请求，这里自定义的使用ajax参数来判断
	public function is_ajax()
	{
		return (isset($_GET['ajax']) || isset($_POST['ajax'])) ? TRUE : FALSE;
	}

	protected function _request_to($method)
	{
		$request_method = $this->is_ajax() ? 'ajax' : ($this->is_get() ? 'get' : 'post');
		$_request_to = '_'.$method.'_'.$request_method;
		$this->$_request_to();
	}
	
	//添加
	public function insert()
	{
		$this->_request_to('insert');
	}
	
	//删除
	public function delete()
	{
		$this->_request_to('delete');
	}
	
	//修改
	public function update()
	{
		$this->_request_to('update');
	}
	
}

//前台控制器
class Font_Controller extends S_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->library('template');
		$this->template->theme(site('theme'));
	}
	
}

//后台控制器
class Admin_Controller extends S_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->library('template');
		$this->template->theme('admin');
		
		
		$directory = trim($this->router->fetch_directory(), '/'); //目录名
		$controller = $this->router->fetch_class(); //控制器类名
		$method = $this->router->fetch_method(); //控制器方法名
		
		//载入session类，便于与前台session区分，我们需要指定prrefix。（默认绑定ip和user_agent的）
		$this->load->library('session');
		$config = array(
			'prefix' => '9jfkad83jad_',
			'save_mode' => 'memcached',
			'memcached_config' => array(
				array(
					'host' => '127.0.0.1',
					'port' => 11211,
					'weight' => 1
				)
			),
			'lifetime' => 3600
		);
		
		//swfupload session处理，否则会出现302等错误
		if($directory == 'admin' && $controller == 'goods' && $method == 'upload' && post('PHPSESSID'))
		{
			session_id(post('PHPSESSID'));
			$config['match_user_agent'] = FALSE;
		}
		
		if ( ! $this->session->config($config)->init())
		{
			log_message('error', '初始化SESSION失败！原因：'.$this->session->get_error());
			show_error('初始化SESSION失败！');
		}
		
		if ($controller != 'home' || ! in_array($method, array('login', 'logout', 'verify')))
		{
			$this->load->model('admin_model');
			if ( ! $this->admin_model->is_login())
			{
				redirect(admin_url('home/login'));
			}
			elseif ( ! $this->admin_model->check_access($directory, $controller, $method))
			{
				msg('你没有权限访问！');
			}
		}

		//记录返回地址
		if (in_array($this->router->fetch_method(), array('index', 'search')))
		{
			$this->session->set('back', $_SERVER['REQUEST_URI']);
		}
	}
	
}